<?php
namespace app\shop\service;

use app\common\model\collect\OrderExpressModel;
use app\common\model\jiyun\OrderModel as JyOrderModel;
use app\common\model\shop\OrderModel as ShopOrderModel;


class SyncOrderService extends BaseService
{
    public static $instance = null;

    public static function instance()
    {
        //如果不存在实例，则返回实例
        if (empty(self::$instance)) {

            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 同步商品到集运
     *
     * @param int $shop_id       商城id
     * @param array $order_ids   订单ids
     * @param int $warehouse_id  仓库id
     * @param int $channel_id    渠道id
     * @param int $country_id    国家id
     */
    public function syncShopGoods (int $shop_id, array $order_ids, int $warehouse_id, int $channel_id, int $country_id, int $account_id)
    {

        $order_add_item = ShopOrderModel::mk()->whereIn('id', $order_ids)->where('sync_status', ShopOrderModel::SYNC_STATUS_NO)->select();
        // 新增
        if (!$order_add_item->isEmpty()) { // 如果有需要推送的
            $this->syncShopGoodsAdd($order_add_item, $warehouse_id, $channel_id, $country_id, $account_id);
        }
        // 编辑
        $order_add_item = ShopOrderModel::mk()->where('shop_id', $shop_id)->where('sync_status', ShopOrderModel::SYNC_STATUS_EDIT)->select();

        if (!$order_add_item->isEmpty()) {

            $this->syncShopGoodsEdit($order_add_item, $warehouse_id, $channel_id, $country_id);
        }

        // 获得要同步的订单头表信息

    }
    /**
     * 同步商品新增逻辑
     *
     * @return void
     */
    private  function  syncShopGoodsAdd ($shop_oder_obj, $warehouse_id, $channel_id, $country_id, $account_id)
    {
        $order_id = [];

        $data     = [];

        foreach ($shop_oder_obj as  $item) {

            $data = [
                'platform_id'   => $item->platform_id,
                'account_id'    => $account_id,
                'shop_account_id'=> $item->account_id,
                'warehouse_id'  => $warehouse_id,
                'shop_id'       => $item->shop_id,
                'order_id'      => $item->id,
                'order_no'      => $item->order_no,
                'country_id'    => $country_id,
                'country'       => $item->country,
                'channel_id'    => $channel_id,
                'express_num'   => count( explode(',',$item->other_order_no)),
                'buy_user'      => $item->buy_user,
                'buy_mobile'    => $item->buy_mobile,
                'user_name'     => $item->user_name,
                'user_mobile'   => $item->user_mobile,
                'user_address'  => $item->user_address,
                'status'        => 0,
                'user_city'     => $item->user_city,
                'user_district' => $item->user_district,
                'user_region'   => $item->user_region,
                'zip_code'      => $item->zip_code,
                'other_order_no' => $item->other_order_no,
                'create_at'     => date("Y-m-d H:i:s"),
            ];

            $id = JyOrderModel::mk()->insertGetId($data);

            $order_id[]     =  $item->id;
            $other_order_no = array_unique(explode(',', $item->other_order_no));
            // 检测物流是否已经 入库
            $order_express = OrderExpressModel::mk()->where('status',OrderExpressModel::STATUS_IN_STORE)->whereIn('order_no', $other_order_no)->select();

            if (!$order_express->isEmpty()) {

                foreach ($order_express as $express) {
                    $express->jiyun_order_id = $id;
                    $express->save();
                }
                $update_data = ['has_express_num'=> count($order_express)] ;
                if (count($other_order_no) == $order_express->count()) {

                    $update_data['status'] = JyOrderModel::STATUS_HAS_NO;
                }

                JyOrderModel::mk()->where('id', $id)->update($update_data);
            }
        }
        // 更新同步状态
        ShopOrderModel::mk()->whereIn('id', $order_id)->update(['sync_status'=>ShopOrderModel::SYNC_STATUS_YES]);
    }

    /**
     * 同步商品新增逻辑
     *
     * @return void
     */
    private  function  syncShopGoodsEdit ($shop_oder_obj, $warehouse_id, $channel_id, $country_id)
    {

        foreach ($shop_oder_obj as  $item) {

            JyOrderModel::mk()->where('order_id', $item->id)->update(['other_order_no' => $item->other_order_no]);
        }
    }


}

?>